package com.prathiba.jumbledword;

import java.util.Arrays;
import java.util.List;

import com.prathiba.wordrepository.WordRepository;

public class Validator {

	private List<String> actualWords;
	private WordRepository wordRepository;

	/**
	 * To check if the user has used the same letters in the jumbled word for
	 * his guess.
	 * 
	 * @param jumbledWord
	 * @param guessedWord
	 * @return boolean
	 */
	public boolean hasSameCharacters(String jumbledWord, String guessedWord) {
		char[] jumbledWordChars = jumbledWord.toCharArray();
		char[] guessedWordChars = guessedWord.toCharArray();
		Arrays.sort(jumbledWordChars);
		Arrays.sort(guessedWordChars);

		return Arrays.equals(jumbledWordChars, guessedWordChars);
	}

	/**
	 * To check if the guessed word is present in the actual words dictionary
	 * 
	 * @param guessedWord
	 * @return boolean
	 */
	public boolean isInDictionary(String guessedWord) {
		actualWords = wordRepository.getActualWords();
		return actualWords.contains(guessedWord);
	}

	public void setWordRepository(WordRepository wordRepository) {
		this.wordRepository = wordRepository;
	}

}
